<template>
  <div class="home">
    <div class="home-body">
      <div class="left"><img src="../assets/register.png" alt="" /></div>
      <div class="right">
        <h1>注册新用户</h1>
        <input
          type="text"
          v-model="user.username"
          placeholder="用户名：6位数以上的字母和数字组成"
        />
        <input type="text" v-model="user.nickname" placeholder="昵称" />
        <input type="password" v-model="user.password" placeholder="密码" />
        <input type="text" v-model="user.school" placeholder="学校" />
        <input placeholder="请输入年龄" v-model="user.age" />
        <select v-model="user.gender">
          <!-- <option v-for="(item,index) in gender" :key="index">请选择性别</option> -->
          <option value="1">男</option>
          <option value="0">女</option>
        </select>
        <h4>已有账户？<router-link to="/">去登录</router-link></h4>
        <button @click="register">注册</button>
      </div>
    </div>
  </div>
</template>
<script>
import { mapMutations } from "vuex";
export default {
  name: "Home",
  data() {
    return {
      user: {
        username: "",
        nickname: "",
        password: "",
        school: "",
        age: "",
        gender: ""
      },
      gender: [
        { sex: "男", id: 1 },
        { sex: "女", id: 0 }
      ]
    };
  },
  mounted() {
    // const{ username, nickname, password, school, age, gender } = this.$store.state.user
  },
  methods: {
    ...mapMutations(["updateRegister"]),
    async register() {
      if (
        this.user.username === "" ||
        this.user.nickname === "" ||
        this.user.password === "" ||
        this.user.school === "" ||
        this.user.age === "" ||
        this.user.gender === ""
      ) {
        alert("请输入完整的信息再完成注册！");
      } else {
        // 调用注册接口，
        const data = await this.yPost("/user/regist", {
          username: this.user.username,
          password: this.user.password,
          nickname: this.user.nickname,
          school: this.user.school,
          age: this.user.age,
          gender: this.user.gender
        });
        console.log(data);
        this.updateRegister(this.user);
        console.log(">>>>>", this.$store);
        this.$router.push({
          name: "Home"
        });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.home {
  width: 1100px;
  height: 700px;
  margin: 5% auto;
  border-radius: 5px;
  box-shadow: 0px 0px 2px 1px rgb(0 0 0 / 20%);
  text-align: center;
  &-body {
    width: 1000px;
    height: 90%;
    margin: auto;
    padding-top: 45px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      width: 500px;
      height: 600px;
    }
  }
}
.right {
  width: 500px;
  margin-left: 30px;
  input {
    width: 70%;
    height: 40px;
    margin-top: 20px;
    border-radius: 10px;
    text-indent: 1em;
    outline: none;
  }
  button {
    width: 70%;
    background-color: rgb(86, 84, 218);
    height: 40px;
    border-radius: 10px;
    font-size: 20px;
    color: #fff;
  }
  select {
    width: 70%;
    height: 40px;
    margin-top: 20px;
    border-radius: 10px;
    font-size: 15px;
  }
}
</style>
